package com.coolcard.framework.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 应用工具类
 */
@Slf4j
public class AppUtil {

    /**
     * 应用初始化时，打印应用信息
     */
    static public void printAppInfo(Class<?> appClass, String[] args) throws UnknownHostException {
        SpringApplication app = new SpringApplication(appClass);
        Environment env = app.run(args).getEnvironment();

        // 打印系统启动信息
        String appName = env.getProperty("spring.application.name");
        String port = env.getProperty("server.port", "8080");
        String contextPath = env.getProperty("server.servlet.context-path", "");
        String hostAddress = InetAddress.getLocalHost().getHostAddress();

        log.info("""

                        ----------------------------------------------------------
                        \t应用 '{}' 已启动! 访问地址为:
                        \t本地地址: \thttp://localhost:{}{}
                        \t外部地址: \thttp://{}:{}{}
                        ----------------------------------------------------------""",
                appName, port, contextPath, hostAddress, port, contextPath);
    }
}
